package com.rrjjg.app.core.config;

import com.rrjjg.app.core.interceptor.SignInterceptor;
import com.rrjjg.app.core.properties.SignJwtProperties;
import com.rrjjg.app.core.sign.DefaultJwtHelper;
import com.rrjjg.app.core.sign.JwtHelper;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import static com.rrjjg.app.core.config.SignConfig.OPEN;
import static com.rrjjg.app.core.config.SignConfig.PRE;

/**
 * ----------------------------------------------------------------------------- <br>
 * 功能 :  <br>
 * ----------------------------------------------------------------------------- <br>
 * 作者 : Vayne <br>
 * 时间 : 2023/4/12 <br>
 */

@AutoConfigureBefore(MvcConfig.class)
@ConditionalOnProperty(name = OPEN,prefix = PRE)
@Configuration
public class SignConfig {

    public static final String PRE = "sign.jwt";
    public static final String OPEN = "open";
    @Bean
    @ConfigurationProperties(PRE)
    public SignJwtProperties signJwtProperties(){
        return new SignJwtProperties();
    }


    @ConditionalOnBean(SignJwtProperties.class)
    @Bean
    public JwtHelper jwtHelper(SignJwtProperties signJwtProperties){
       return new DefaultJwtHelper(signJwtProperties);
    }

    @ConditionalOnBean(JwtHelper.class)
    @Bean
    public SignInterceptor signInterceptor(JwtHelper jwtHelper){
        return new SignInterceptor(jwtHelper);
    }
}
